# Maintainer: Wu Zhenyu <wuzy01@qq.com>
_realname=fzf

pkgbase="mingw-w64-${_realname}"
pkgname="$MINGW_PACKAGE_PREFIX-${_realname}"
pkgver=0.35.1
pkgrel=1
pkgdesc='Command-line fuzzy finder (mingw-w64)'
arch=('any')
mingw_arch=(mingw64 ucrt64 clang64)
url='https://github.com/junegunn/fzf'
license=('spdx:MIT')
makedepends=(
  "$MINGW_PACKAGE_PREFIX-go"
  "$MINGW_PACKAGE_PREFIX-cc"
)
source=("https://github.com/junegunn/fzf/archive/refs/tags/${pkgver}.tar.gz")
sha256sums=('d59ec6f2b6e95dad53bb81f758471e066c657be1b696f2fe569e1a9265dda8fe')

prepare() {
  cd "$srcdir/${_realname}-${pkgver}"
  sed -i 's/-w /-w -linkmode external /' Makefile
}

build() {
  cd "$srcdir/${_realname}-${pkgver}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOFLAGS="-trimpath -mod=readonly -modcacherw -buildvcs=false"
  case "${CARCH}" in
    i686|x86_64)
      GOFLAGS+=" -buildmode=pie"
      ;;
  esac

  FZF_VERSION=${pkgver} FZF_REVISION=tarball make
}

package() {
  cd "$srcdir/${_realname}-${pkgver}"

  ## Man page
  install -Dm644 man/man1/fzf.1 "$pkgdir"$MINGW_PREFIX/share/man/man1/fzf.1
  install -Dm644 man/man1/fzf-tmux.1 "$pkgdir"$MINGW_PREFIX/share/man/man1/fzf-tmux.1

  ## License
  install -Dm644 LICENSE "$pkgdir"$MINGW_PREFIX/share/licenses/fzf/LICENSE

  ## Binaries
  install -dm755 "$pkgdir"$MINGW_PREFIX/bin
  install -m755 bin/fzf-tmux "$pkgdir"$MINGW_PREFIX/bin/fzf-tmux.exe
  install -Dm755 target/fzf-* "$pkgdir"$MINGW_PREFIX/bin/fzf.exe

  ## Completion and keybindings
  install -dm755 "$pkgdir"$MINGW_PREFIX/share/fzf
  install -m644 shell/*.bash shell/*.zsh "$pkgdir"$MINGW_PREFIX/share/fzf

  ## Fish keybindings
  install -Dm644 shell/key-bindings.fish "$pkgdir"$MINGW_PREFIX/share/fish/vendor_functions.d/fzf_key_bindings.fish

  ## Vim plugin
  install -Dm644 doc/fzf.txt "$pkgdir"$MINGW_PREFIX/share/vim/vimfiles/doc/fzf.txt
  install -Dm644 plugin/fzf.vim "$pkgdir"$MINGW_PREFIX/share/vim/vimfiles/plugin/fzf.vim
}
